package vampirism.core.shared;

import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;

/**
 * There used to be Players/NPCs, now there are only Players
 * But i didn't merge Body and Player class, if case of future NPCs
 * So it's suggested to put Player-specific functionality in Player class
 * And Player/NPC functionality in Body class
 * @author PC
 *
 */
public class Body {
	
	//Position and size of this body
	public Rectangle pos = new Rectangle();
	
	public short level = 1;
	public short exp = 0;
	public short cubits = 0;
	public short health = 50;
	public short energy = 50;

	//Client and server has 'bodies' List (a list of online bodies), this is the index of that list
	//Offline bodies have index -1
	public short index;
	
	//0 = Warrior, 1 = Priest, 2 = Archer, 3 = Rogue, 4 = Mage
	public short type;  
	
	//Name - is unique for all players
    public String name;
	
	//Animation time
	public float animTime;
	
	//The last direction that this body was facing towards (for rendering purposes)
	public byte lastDir;
	
	//The current location of the body (within map[x][y])
	public Index tileIndex = new Index(0,0);
	
	//The last time when this body was moved
	//Movement happens both - in client side and in server side
	public Long lastMoveTime = new Long(0);
	
	//The last time a projectile was fired
	public Long lastShootTime = new Long(0);
	
	//Has this body turned into vampire?
	public boolean vampire = false;
	
	//Is this body the mega-super-duper-alpha-vampire?
	public boolean alpha = false;
	
	//Has this body died?
	public boolean dead = false;
	
	//0 = right, 1 = left, 2 = down, 3 = up
	public boolean[] moving = new boolean[4];
    
    //0:  Att;  //Attack       - determines hit chance (relative to defence)
    //1:  Def;  //Defence      - determines dodge chance (relative to attack)
    //2:  Dex;  //Dexterity    - determines speed of all attacks (magical/physical)
    //3:  Agi;  //Agility      - determines movement speed
    //4:  Vit;  //Vitality     - determines hp regen
    //5:  Int;  //Intelligence - determines mp regen
    //6:  HpMax //Maximum HP   - determines the maximum Health (solid number)
    //7:  MpMax //Maximum MP   - determines the maximum Energy (solid number)
    //8:  Total Protection     - determines how much damage is subtracted from each attack (solid number)
    //9:  Critical chance      - determines critical chance (%) (2x damage?)
    //10: Pierce chance        - determines if the projectile passes through target
    //11: Damage recoil        - determines amount of damage (solid number) that is dealt to attacker after receiving a hit
    //12: Exp gain bonus       - experience gain bonus %
    //13: Item find bonus      - item find bonus %
    //14: Health gain on hit   - solid number
    //15: Energy gain on hit   - solid number
    
    public short[] statsBase = new short[16];  //Base stats - stats are reset to these values in each recalculate()
    public short[] statsGain = new short[16];  //Stats gained from equipment / spells / skills
    public short[] statsTotal= new short[16];  //The end result for each stat after all boosts/gains/calculations
    
    
    public void initStats(){
    	
    	pos.x = Network.rand.nextInt(60) - Network.rand.nextInt(60);
    	pos.y = Network.rand.nextInt(60) - Network.rand.nextInt(60);
    	
    	vampire = false;
    	dead = false;
    	alpha = false;
    	
    	//The default values - equal to all classes.
    	statsBase[0] = 10;
    	statsBase[1] = 10;
    	statsBase[2] = 10;
    	statsBase[3] = 20;
    	statsBase[4] = 10;
    	statsBase[5] = 10;
    	
    	statsBase[6] = 50;
    	statsBase[7] = 50;
    	
    	if(type == 0){//Warrior
    		statsBase[0] = 15;
    		statsBase[1] = 15;
    		statsBase[8] = 3;
    		statsBase[9] = 3;
    	}else
    	if(type == 1){//Priest
    		statsBase[5] = 15;
        	statsBase[6] = 70;
        	statsBase[7] = 70;
    	}else
    	if(type == 2){//Archer
        	statsBase[2] = 15;
        	statsBase[3] = 15;
        	statsBase[13] = 5;
    	}else
    	if(type == 3){//Rogue
    		statsBase[3] = 15;
    		statsBase[4] = 15;
    		statsBase[6] = 70;
    	}else
    	if(type == 4){//Mage
    		statsBase[2] = 15;
    		statsBase[5] = 15;
    		statsBase[10] = 5;
    	}
    	
    	recalculateStats();
    }
    
	public void recalculateStats() {
    	for(int i = 0; i < statsBase.length; i++){
    		statsTotal[i] = (short)(statsBase[i]+statsGain[i]);
    	}
    	health = statsTotal[6];
    	energy = statsTotal[7];
	}
    
    public void dealDamage(int damage){
    	health -= damage;
    	//TODO check death
    }

    
    public short getShootDelay(){
    	//If somehow dexterity is ever below 10 -> delay is 1500 milliseconds
    	if(statsTotal[2] < 10)return 1500;
    	
    	return (short)(10000/statsTotal[2]);
    	
    	//10 dexterity -> 1000 Millisecond delay -> 1 bullet per second
    	//20 dexterity -> 500  Millisecond delay -> 2 bullet per second
    	//40 dexterity -> 250  Millisecond delay -> 4 bullet per second
    	//80 dexterity -> 125  Millisecond delay -> 8 bullet per second
    }
    
}
